package app.com.itcast.biyesheji.Adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import app.com.itcast.biyesheji.Activity.ShopActivity;
import app.com.itcast.biyesheji.Bean.MyBean;
import app.com.itcast.biyesheji.R;

public class MyAdapter extends BaseAdapter {

    List list;
    Context context;
    private int userid;
    public MyAdapter(List list, Context context) {
        this.list = list;
        this.context = context;

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.shouye_list, parent, false);
        //这里写listview里面item的控件，findViewById前面要加上(view.)。
        TextView name = view.findViewById(R.id.name);
        TextView text = view.findViewById(R.id.text);
        TextView price = view.findViewById(R.id.price);
        ImageView icon = view.findViewById(R.id.image);
        LinearLayout linearLayout = view.findViewById(R.id.tiaozhuan);
        Button add = view.findViewById(R.id.add_shop);
        //把DataBean的数据从List里面取出来。
        MyBean.ListBean listBean = (MyBean.ListBean) list.get(position);
        // 获取当前项的数据中的某个属性的值，将其设置为文本框控件的属性，完成数据的呈现。
        name.setText(listBean.getName());
        text.setText(listBean.getText());
        price.setText(listBean.getPrice()+"");
        // 如果是图片。
        Picasso.with(context).load(context.getResources().getString(R.string.api_ip)+listBean.getImage()).into(icon);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyBean.ListBean bean = (MyBean.ListBean) list.get(position);
                Log.e("TAG", "onItemClick: "+"位置"+position+"数据"+bean );
                Intent intent = new Intent();
                intent.setClass(context, ShopActivity.class);
                intent.putExtra("id",bean.getId());
                context.startActivity(intent);
            }
        });
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences userinfo = context.getSharedPreferences("data",context.MODE_PRIVATE);
                userid = userinfo.getInt("userid",0);
                RequestQueue requestQueue = Volley.newRequestQueue(context);
                String url =context.getResources().getString(R.string.api_ip)+"/car/momo/addone";
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("userid", userid);
                    jsonObject.put("name", listBean.getName());
                    jsonObject.put("price", listBean.getPrice());
                    jsonObject.put("num", listBean.getNum());
                    jsonObject.put("image", listBean.getImage());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Log.e("TAG", "onResponse: " + jsonObject);
                        /*处理结果数据*/
                        // 1）讲json对象转换为java对象（需要使用GsonFormat插件根据返回数据，创建结果处理类MyBean）
                        Gson gson = new Gson();
                        MyBean myBean = gson.fromJson(jsonObject.toString(), MyBean.class);
                        if(myBean.isSuccess()){
                            Toast.makeText(context, "添加商品", Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(context, "失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                    }
                });
                requestQueue.add(jsonObjectRequest);

            }
        });

        return view;
        // 返回填充了内容的项视图对象。
    }
}

